// ServerApps Loader File for PCMod (SERVER)

// Define apps table
PCMod.ServerApps = {}

// Add the clientside file
AddCSLuaFile( "cl_serverapps.lua" )

// Add in the base app
local app = {}
app.Name = "Base ServerApp"
app.AppName = "base"
function app:Useable() return false end
function app:Initialize() end
function app:Init() end
function app:StartUp() end
function app:ShutDown() end
PCMod.ServerApps[ "base" ] = app

// Load all apps
local cnt = 0
for k, v in pairs (file.FindInLua( "serverapps/app_*")) do
	// Add to our count
	cnt = cnt + 1
	
	// Redefine the app
	APP = {}
	
	// Include the file
	include( "serverapps/" .. v )
	
	// Insert the app
	PCMod.ServerApps[ APP.AppName ] = APP
end

// Send app data to newly spawned players
hook.Add( "PlayerSpawn", "OnSpawn_AppList", function( ply )
	if (ply.AppListSent) then return end
	ply.AppListSent = true
	//timer.Create( "OnSpawn_SendAppList_" .. ply:Nick(), 2, 1, function( ply )	
		local tmp = {}
		// Filter all apps that are unuseable,  now that we have given chance for all the other addons to load
		for k, v in pairs( PCMod.ServerApps ) do
			if (v:Useable()) then
				table.insert( tmp, { k, v } )
				// PCMod:Print( k .. " useable!" )
			end
		end
		umsg.Start( "_pcmod_sapplist", ply )
			umsg.Short( #tmp )
			for _, v in pairs( tmp ) do
				umsg.String( v[1] .. "#" .. v[2].Name )
			end
		umsg.End()
	// end, ply )
end )